#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "gameObjects/abstractgameobject.h"
#include "document.h"
#include "application.h"
#include "editorview.h"
#include "gameobjectseditorwidget.h"
#include "gameobjectpropertieswidget.h"
#include "modes/editormode.h"
#include "modes/editormodemanager.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    initGui();
	createConnections();
    showMaximized();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initGui()
{
	m_editMenu = new QMenu("Edit");

	m_modeMenu = new QMenu("Mode");
	m_objectsModeAct = m_modeMenu->addAction("Object mode");
	m_objectsModeAct->setCheckable(true);
	m_levelEditorModeAct = m_modeMenu->addAction("Level mode");
	m_levelEditorModeAct->setCheckable(true);
	m_previewModeAct = m_modeMenu->addAction("Preview mode");
	m_previewModeAct->setCheckable(true);
	QActionGroup* pModeGroup = new QActionGroup(this);
	pModeGroup->setExclusive(true);
	pModeGroup->addAction(m_objectsModeAct);
	pModeGroup->addAction(m_levelEditorModeAct);
	pModeGroup->addAction(m_previewModeAct);

	m_pModeToolBar = new QToolBar("Editor mode");
	m_pModeToolBar->addAction(m_objectsModeAct);
	m_pModeToolBar->addAction(m_levelEditorModeAct);
	m_pModeToolBar->addAction(m_previewModeAct);
	addToolBar(Qt::RightToolBarArea, m_pModeToolBar);

	reinitializeGui();
}

void MainWindow::updateGui()
{
    ACQUIRE_DOC();
    ui->actionFileSave->setEnabled(doc != 0);
    ui->actionFileSaveAs->setEnabled(doc != 0);
    ui->actionFileClose->setEnabled(doc != 0);
}

void MainWindow::onDocumentChanged(QPointer<Document> document)
{
	if(document)
	{
		// enter object mode
		m_objectsModeAct->trigger();
	}
	else
	{
		Application::instance().modeManager()->changeState(QPointer<AbstractState>());
	}

	updateGui();
}

void MainWindow::createConnections()
{
	connect(ui->actionFileNew, &QAction::triggered, 
		this, &MainWindow::onFileNew);
	connect(ui->actionFileClose, &QAction::triggered, 
		this, &MainWindow::onFileClose);
	connect(m_objectsModeAct, &QAction::triggered, 
		this, &MainWindow::onModeChanged);
	connect(m_levelEditorModeAct, &QAction::triggered, 
		this, &MainWindow::onModeChanged);
	connect(m_previewModeAct, &QAction::triggered, 
		this, &MainWindow::onModeChanged);
}

void MainWindow::onFileNew()
{
	Application::instance().createDocument();
}

void MainWindow::onFileClose()
{
	Application::instance().closeDocument();
}

void MainWindow::onModeChanged()
{
	QAction* pMode = qobject_cast<QAction*>(sender());
	Q_ASSERT(pMode);

	if(pMode == m_objectsModeAct)
	{
		Application::instance().modeManager()->changeState("ObjectEditorMode");
	}
	else if(pMode == m_levelEditorModeAct)
	{
		Application::instance().modeManager()->changeState("LevelEditorMode");
	}
}

void MainWindow::reinitializeGui()
{
	menuBar()->clear();
	ui->mainToolBar->clear();

	menuBar()->addMenu(ui->menuFile);
	ui->mainToolBar->addAction(ui->actionFileNew);
	ui->mainToolBar->addAction(ui->actionFileSave);
	ui->mainToolBar->addAction(ui->actionFileSaveAs);
	ui->mainToolBar->addAction(ui->actionFileClose);

	ACQUIRE_DOC();
	m_pModeToolBar->setVisible(doc != 0);

	if(doc)
	{
		menuBar()->addMenu(m_modeMenu);
	}
}
